package com.stadium.web.controller;

import com.fastsite.common.api.ApiResult;
import com.fastsite.utils.EncryptUtil;
import com.fastsite.utils.StringUtils;
import com.fastsite.utils.validator.annotation.Mobile;
import com.stadium.common.StadiumConstant;
import com.stadium.web.domain.User;
import com.stadium.web.domain.Venue;
import com.stadium.web.service.UserService;
import com.stadium.web.service.VenueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "登陆注册")
@RestController
@Validated
@RequestMapping("sys")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private VenueService venueService;

    @ApiOperation(value = "登陆", produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name="username",value="用户名",required=false,paramType="body",dataType="Long"),
//            @ApiImplicitParam(name="password",value="密码",required=false,paramType="body",dataType="String")
//    })
    @PostMapping("login")
    public ApiResult login(@Valid @RequestBody LoginForm from){
        User user = userService.findByPhoneAndPassword(from.getUsername(), EncryptUtil.encrypt(from.getPassword(),StadiumConstant.SALT));
        if(StringUtils.isNull(user)){
            return ApiResult.err("用户名密码错误");
        }
        Map result = new HashMap<String,Object>();
        String token = userService.createToken(user);
        List<Venue> venues =venueService.findByUserId(user.getId());
        result.put("user",user);
        result.put("token",token);
        result.put("venues",venues);
        return ApiResult.ok(result,"登陆成功");
    }

    @ApiOperation(value = "注册", produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping("register")
    public ApiResult register(@Valid @RequestBody RegisterForm from){
        User user  = userService.findByUsername(from.getPhone());
        if(user != null){
            return ApiResult.err("手机号以注册");
        }

        user = new User();
        user.setPassword(EncryptUtil.encrypt(from.getPassword(),StadiumConstant.SALT));
        user.setName(from.getName());
        user.setPhone(from.getPhone());
        user = userService.save(user);
        return ApiResult.ok(user,"注册成功");
    }


}

@Data
@ApiModel("登陆Form")
class LoginForm{
    @Mobile
    @ApiModelProperty("用户名")
    @NotBlank(message = "用户名不能为空")
    private String username;
    @NotBlank(message = "密码不能为空")
    @ApiModelProperty("密码")
    private String password;
}

@Data
@ApiModel("注册Form")
class RegisterForm{
    @ApiModelProperty("昵称")
    @NotBlank(message = "昵称不能为空")
    private String name;
    @Mobile
    @ApiModelProperty("用户名")
    @NotBlank(message = "用户名不能为空")
    private String phone;
    @NotBlank(message = "密码不能为空")
    @ApiModelProperty("密码")
    private String password;
}
